﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DSAManager.Core;
using DSAManager.Models.Output;
using DSAManager.Utils;

namespace DSAManager
{
    public partial class FrmSetting : Form
    {
        public FrmSetting()
        {
            InitializeComponent();
        }

        private void FrmSetting_Load(object sender, EventArgs e)
        {
            //loadconfig();
        }

        void loadconfig()
        {
            BackgroundWorker bk = new BackgroundWorker();
            bk.DoWork += delegate
                             {
                                 var result = Api.LoadConfigs();
                                 if (result.ok)
                                 {
                                     foreach (var item in result.data)
                                     {
                                         if (item.ckey == "client_id") ControlInvokeHelper.Set(txt_client_id, "Text", item.cvalue);
                                         if (item.ckey == "client_secret") ControlInvokeHelper.Set(txt_client_secret, "Text", item.cvalue);
                                         if (item.ckey == "ACCESS_KEY") ControlInvokeHelper.Set(txt_ACCESS_KEY, "Text", item.cvalue);
                                         if (item.ckey == "SECRET_KEY") ControlInvokeHelper.Set(txt_SECRET_KEY, "Text", item.cvalue);
                                         if (item.ckey == "redirect_uri") ControlInvokeHelper.Set(txt_redirect_uri, "Text", item.cvalue);
                                         if (item.ckey == "share_dir") ControlInvokeHelper.Set(txt_sharedir, "Text", item.cvalue);
                                         if (item.ckey == "down_dir") ControlInvokeHelper.Set(txt_downdir, "Text", item.cvalue);
                                         if (item.ckey == "save_mode") ControlInvokeHelper.Set(cbx_savemode, "SelectedIndex", IntExtension.Parse(item.cvalue));

                                     }
                                 }
                             };
            bk.RunWorkerAsync();
        }

        private void FrmSetting_Shown(object sender, EventArgs e)
        {
            loadconfig();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            List<ConfigItem> configItems = new List<ConfigItem>();
            configItems.Add(new ConfigItem() { ckey = "client_id", cvalue = txt_client_id.Text });
            configItems.Add(new ConfigItem() { ckey = "client_secret", cvalue = txt_client_secret.Text });
            configItems.Add(new ConfigItem() { ckey = "ACCESS_KEY", cvalue = txt_ACCESS_KEY.Text });
            configItems.Add(new ConfigItem() { ckey = "SECRET_KEY", cvalue = txt_SECRET_KEY.Text });
            configItems.Add(new ConfigItem() { ckey = "redirect_uri", cvalue = txt_redirect_uri.Text });
            configItems.Add(new ConfigItem() { ckey = "share_dir", cvalue = txt_sharedir.Text });
            configItems.Add(new ConfigItem() { ckey = "down_dir", cvalue = txt_downdir.Text });
            configItems.Add(new ConfigItem() { ckey = "save_mode", cvalue = cbx_savemode.SelectedIndex.ToString() });

            var result = Api.SaveConfigs(configItems);
            if (result.ok)
            {
                AppContext.client_id = txt_client_id.Text;
                AppContext.redirect_uri = txt_redirect_uri.Text;
                AppContext.share_dir = txt_sharedir.Text;
                AppContext.down_dir = txt_downdir.Text;
                AppContext.save_mode = cbx_savemode.SelectedIndex;
                MsgBox.Alert("保存成功!");
            }
            else
            {
                MsgBox.Alert(result.msg);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var result = Api.CreateWeixinMenu();
            if (result.ok)
            {
                if (result.data.errcode == 0)
                {
                    MsgBox.Alert("菜单更新成功!");
                }
                else
                {
                    MsgBox.Alert("菜单更新失败!");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            decimal width = 948;
            decimal height = 1280;
            decimal WidgeWidth = 346;
            decimal WidgeHeight = 467;
            decimal BeginX = 0;
            decimal BeginY = 121;
            decimal EndX = 346;
            decimal EndY = 467;


            var radio = width / WidgeWidth;
            //获取裁剪框大小
            decimal CutWidth = (EndX - BeginX) * radio;
            decimal CutHeight = (EndY - BeginY) * radio;

            //计算偏移
            BeginX = BeginX * radio;
            BeginY = BeginY * radio;

            var url = "http://7xj84r.com2.z0.glb.qiniucdn.com/eba0d526c2064e228c3bb8dbfc8f8022.jpg";
            url = url + "?imageMogr2/rotate/0/crop/!" + CutWidth + "x" + CutHeight + "a" + BeginX + "a" + BeginY;
        }
    }
}
